package com.example.nioprc.infrastructure.registry;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RpcServiceRegistry implements ServiceRegistry{
    //服务名称到服务实例的映射
    private  final Map<String,Object> serviceMap=new ConcurrentHashMap<>();
    @Override
    public void registerService(Class<?> interfaceClass, Object serviceInstance) {
        String serviceName=interfaceClass.getName();
        serviceMap.put(serviceName,serviceInstance);
    }

    @Override
    public Object getService(String serviceName) {
        Object service = serviceMap.get(serviceName);
        if(service==null){
            throw new RuntimeException("没有找到服务："+serviceName);
        }
        return service;
    }

    @Override
    public Map<String, Object> getAllServices() {
        return new HashMap<>(serviceMap);
    }
}
